<?php
/*
 module:		发布管理
 create_time:	2022-03-09 16:30:59
 author:	陕西格创
 contact:		
*/

namespace app\api\controller;

use app\api\model\ZhMatchmakerLevel as ZhMatchmakerLevelModel;
use think\facade\Db;
use utils\wechart\TemplateService;

class ZhMatchmakerLevel extends Common
{



    //获取所有级别
    function getLevelList(){
        $res =ZhMatchmakerLevelModel::where(['status' => 1])->order(['createtime' => 'desc'])->select();
        return $this->ajaxReturn($this->successCode, '返回成功', $res);
    }

    //获取所有招商级别
    function getZsLevelList(){
        $res =ZhMatchmakerLevelModel::where(['status' => 1,"is_zs"=>1])->order(['createtime' => 'desc'])->select();
        return $this->ajaxReturn($this->successCode, '返回成功', $res);
    }


    //佳人同意谢媒费
    function update(){
        $id = $this->request->post('id');
        $a_id = $this->request->post('a_id');
        $status = $this->request->post('status');
        $wxapp_id = $this->request->post('wxapp_id');
        $matchmaker_meet=ZhMatchmakerMeetModel::find($id);
        $matchmaker=ZhMatchmakerLevelModel::where("matchmaker_id",$matchmaker_meet["m_id"])->find();
        if(!$matchmaker_meet){
            return $this->ajaxReturn($this->errorCode, "数据不存在，请传正确id");
        }


        if(!in_array($status,[1,2])){
            return $this->ajaxReturn($this->errorCode, "status参数不正确");
        }
        $data['a_id'] =$a_id;
        $data['status'] =$status;
        $data['updatetime'] =time();
        $res=ZhMatchmakerMeetModel::where(["id"=>$id])->update($data);
        if($res){
            //发起通知
            $notice["wxapp_id"]=$wxapp_id;
            $notice["u_id"]=0;//发送人id
            $notice['s_id'] =$matchmaker["u_id"];//接收人
            $notice['create_time'] =date("Y-m-d H:i:s");
            $notice['status'] =1;
            $c_id = ZhChat::insertGetId($notice);
            if($c_id){
                if($status==1){
                    $content="佳人/家长已同意您在".date("Y-m-d H:i:s",$matchmaker_meet["createtime"])."发起的“线下相亲邀请”";
                }elseif($status==2){
                    $content="佳人/家长已拒绝您在".date("Y-m-d H:i:s",$matchmaker_meet["createtime"])."发起的“线下相亲邀请”";
                }
                $log["wxapp_id"]=$wxapp_id;
                $log["chat_id"]=$c_id;
                $log["type"]="text";
                $log["content"]=$content;
                $log["add_time"]=date("Y-m-d H:i:s");
                $log["u_id"]=$matchmaker["u_id"];
                $log["is_read"]=0;
                $log["record_time"]=date("H:i",time());
                $c_log_id=ZhChatLog::insertGetId($log);
            }
            return $this->ajaxReturn($this->successCode, '操作成功');
        }else{
            return $this->ajaxReturn($this->errorCode, '操作失败');
        }
    }


}

